<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 管理员登陆类
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Login extends Admin_Controller{
	
	function __construct(){
		parent::__construct();	
		$this->load->model('JayphaDB');
		$this->adminCurrentTemplate = $this->JayphaDB->getConfigValue('adminCurrentTemplate');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}

	function index(){
		if($this->input->server('REQUEST_METHOD')=='POST'){
			session_start();
			$data['errorTitle'] = '登陆失败';
			if(!isset($_SESSION['adminCode'])){
				$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
					'width'=>280,
					'height'=>110,
					'title'=>'登陆错误',
					'content'=>'<span class="red">对不起，验证码已过期，请重新获取！</span>',
					'url'=>'history.back();',
					'target'=>'',
					'conver'=>true,
					'resize'=>false,
					'drag'=>true,
					'btns'=>true,
					'btnText'=>'返回',
					'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			$userName = $this->input->post('userName');
			$password = $this->input->post('password');
			$chkCode = $this->input->post('chkCode');
			if(strlen($userName)==0){
				$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
					'width'=>300,
					'height'=>110,
					'title'=>'登陆错误',
					'content'=>'<span class="red">对不起，用户名不能为空，请输入用户名！</span>',
					'url'=>'history.back();',
					'target'=>'',
					'conver'=>true,
					'resize'=>false,
					'drag'=>true,
					'btns'=>true,
					'btnText'=>'返回',
					'rightClose'=>false
				));
			}else if(strlen($password)==0){
				$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
					'width'=>280,
					'height'=>110,
					'title'=>'登陆错误',
					'content'=>'<span class="red">对不起，密码不能为空，请输入密码！</span>',
					'url'=>'history.back();',
					'target'=>'',
					'conver'=>true,
					'resize'=>false,
					'drag'=>true,
					'btns'=>true,
					'btnText'=>'返回',
					'rightClose'=>false
				));
			}else if(strlen($chkCode)==0){
				$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
					'width'=>300,
					'height'=>110,
					'title'=>'登陆错误',
					'content'=>'<span class="red">对不起，验证码不能为空，请输入验证码！</span>',
					'url'=>'history.back();',
					'target'=>'',
					'conver'=>true,
					'resize'=>false,
					'drag'=>true,
					'btns'=>true,
					'btnText'=>'返回',
					'rightClose'=>false
				));
			}else if(strtolower($chkCode)!=strtolower($_SESSION['adminCode'])){
				$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
					'width'=>300,
					'height'=>110,
					'title'=>'登陆错误',
					'content'=>'<span class="red">对不起，验证码不正确，请重新输入！</span>',
					'url'=>'history.back();',
					'target'=>'',
					'conver'=>true,
					'resize'=>false,
					'drag'=>true,
					'btns'=>true,
					'btnText'=>'返回',
					'rightClose'=>false
				));
				unset($_SESSION['adminCode']);
			}else{
				unset($_SESSION['adminCode']);
				$tvName = 'admin';
				$where = array(
					'userName'=>$userName,
					'userPwd'=>strtoupper(md5($password)),
					'disabled'=>false
				);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where)==0){
					$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
						'width'=>320,
						'height'=>110,
						'title'=>'登陆错误',
						'content'=>'<span class="red">对不起，用户名或密码有误，请查证后再试！</span>',
						'url'=>'history.back();',
						'target'=>'',
						'conver'=>true,
						'resize'=>false,
						'drag'=>true,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
					));
				}else{
					$adminUserId = 0;
					$adminPermission = '';
					$isOnline = false;
					foreach($this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$tvName,$where,'',true) as $admin){
						$adminUserId = $admin->id;
						$adminPermission = $admin->permission;
						$isOnline = true;
					}
					$this->db->trans_start();
					$updateData = array('online'=>$isOnline);
					$updateWhere = array('id'=>$adminUserId);
					$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$updateWhere);
					$tvName = 'admin_login_log';
					$insertLoginLogData = array(
							'adminId'=>$adminUserId,
							'loginUserName'=>$userName,
							'loginIP'=>$this->input->ip_address()
					);
					$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$insertLoginLogData,true);
					
					$operationData = array(
						'adminId'=>$adminUserId,
						'title'=>'用户登陆成功',
						'url'=>$this->input->server('REQUEST_URI'),
						'remark'=>'用户登陆成功'
					);
					$this->JayphaDB->setAdminOperation($operationData);
					$this->db->trans_complete();
					if($this->db->trans_status()){
						$adminSession = array(
							'adminUserId'=>$adminUserId,
							'adminUserName'=>$userName,
							'adminPermission'=>$adminPermission,
							'adminIsOnline'=>$isOnline
						);
						$this->session->set_userdata($adminSession);
						$_SESSION['adminIsOnline']=$this->session->userdata('adminIsOnline');
						redirect(site_url('admin/index'));
						return false;
					}else{
						$data['errorScript'] = array('lhgdialog_alert_mess_top'=>array(
							'width'=>180,
							'height'=>98,
							'title'=>'登陆日志更新错误',
							'content'=>'<span class="red">对不起，登陆日志更新失败！</span>',
							'url'=>'history.back();',
							'target'=>'',
							'conver'=>true,
							'resize'=>false,
							'drag'=>true,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
						));
						$data['errorMsg'] = "登陆日志更新失败！";
					}
				}
			}
			$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
		}else{
			$data['loginTitle'] = '管理员登陆';
			$this->load->view("admin/$this->adminCurrentTemplate/login",$data);
		}
	}

	/**
	 * 管理员登陆表单
	 */
	function loginForm(){
		$data['pageTitle'] = '管理员登陆';
		$this->load->view("admin/$this->adminCurrentTemplate/loginForm",$data);
	}
	
	/**
	 * 注销登陆
	 */
	function logout(){
		session_start();
		unset($_SESSION['adminIsOnline']);
		$operationData = array(
			'adminId'=>$this->session->userdata('adminUserId'),
			'title'=>'用户注销登陆',
			'url'=>$this->input->server('REQUEST_URI'),
			'remark'=>'用户成功注销登陆'
		);
		$this->session->sess_destroy();
		$this->JayphaDB->setAdminOperation($operationData);
		redirect(site_url('admin/login'));
	}	
}

/* End of file login.php */
/* Location: ./application/controllers/admin/login.php */